package pocker;

import pocker.exceptions.InvalidPlayerCardException;
import pocker.exceptions.InvalidPlayerNameException;
import pocker.exceptions.OutOfCardsException;

/**
 * Created with IntelliJ IDEA.
 * User: gia
 * Date: 23.08.2012
 * Time: 09:25
 */
public class Player {
    public static final String EMPTY_STRING = "";
    private String name;
    private int card1, card2, bet;

    public Player(int card1, int card2, String name) throws InvalidPlayerCardException, InvalidPlayerNameException {

        if (isNotValidName(name))
            throw new InvalidPlayerNameException("Invalid player name: " + name);
        if (isNotValidCard(card1) || isNotValidCard(card2))
            throw new InvalidPlayerCardException("Invalid cards: " + card1 + " ans " + card2);
        setCardsInOrder(card1, card2);
        this.name = name;
    }

    private void setCardsInOrder(int card1, int card2) {
        if (card1 > card2) {
            this.card1 = card1;
            this.card2 = card2;
            return;
        }
        this.card1 = card2;
        this.card2 = card1;
    }

    public Player(IDealer dealer, String name) throws InvalidPlayerNameException, InvalidPlayerCardException, OutOfCardsException {
        this(dealer.deal(), dealer.deal(), name);
    }

    public int getCard1() {
        return card1;
    }

    public int getCard2() {
        return card2;
    }

    private boolean isNotValidCard(int card) {

        return card < 2 || card > 14;
    }

    private boolean isNotValidName(String name) {

        return name == null || name.equals(EMPTY_STRING);
    }

    public String getName() {
        return name;
    }

    public boolean hasEqualCards() {
        return card1 == card2;
    }

    public void setBet(int bet) {
        this.bet = bet;
    }

    public int getBet() {
        return bet;
    }

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", card1=" + card1 +
                ", card2=" + card2 +
                '}';
    }
}
